const express=require('express');
const businessRouter=express.Router();
const businessSer=require('../../services/businessService');
const userSer=require('../../services/userService');
const tokenSer=require('../../util/token');

//商家获取封面信息
businessRouter.get('/lists',async (req,res)=>{
    let {category,pagenum ,pagesize}=req.query;
    let result= await businessSer.findAll(['id','business','avatarCoverImg','stepOn','support'],category,pagenum ,pagesize);
    if(result.lists==false) return res.send({
        data:{},
        meta:{
            msg:"该数据不存在",
            status: 404
        }
    })
    res.send({
        data:{
            ...result
        },
        meta:{
            msg:"数据获取成功",
            status: 200
        }
    })
})
//获取商家详细信息
businessRouter.get('/more',async (req,res)=>{
    let {id}=req.query;
    let [result] = await businessSer.findData(id);
    if(result==undefined) return res.send({
        data:{},
        meta:{
            msg:"该数据不存在",
            status: 404
        }
    })
    res.send({
        data:{
            result
        },
        meta:{
            msg:"数据获取成功",
            status: 200
        }
    })
})
//点赞/踩
businessRouter.put('/support',async (req,res)=>{
    let {id,flag}=req.body;
    // 注意：node中 !0与!1皆是false  三元中赋值变量0与1都走正值 
    flag=flag==true?true:false;
    // 获取用户名
    let {decode}= await tokenSer.en(req.headers.authorization);
    // 获取用户是否操作过
    let ulist= await userSer.businessSupport(decode.data);
    ulist.business=JSON.parse(ulist.business);
    if(ulist.business==null) ulist.business=[];
    // 如果用户操作过
    let uflag= ulist.business.some((val)=> val.id==id)
    // 操作返回结果
    let resObj=null;
    if(uflag){
        // 用户已操作
        let uflag2= ulist.business.some((val)=> val.flag==flag&&val.id==id)
        // 判断用户此次是否是重复操作
        if(uflag2){
            // 是重复操作  不不进行操作
            res.send({
                data:{},
                meta:{msg:"重复操作",status: 200}
            })   
        }else{
            //不是重复操作
            resObj=await businessBo(id,flag,true);
        }
    }else{
        // 用户未操作
        resObj=await businessBo(id,flag);
    }
    /***         ***/
        async function businessBo(id,flag,flg=false){
            let [result]=await businessSer.findData(id);
            if(result==undefined) return res.send({
                data:{},
                meta:{
                    msg:"该数据不存在",
                    status: 404
                }
            })
            flag==true?++result.support:++result.stepOn;
                        // 赞或踩写入返回结果
            if(flg) {
                flag==true?--result.stepOn:--result.support;
                            // 赞或踩写入返回结果
            };
            let [result2] = await businessSer.upDateSupport(id,{support:result.support,stepOn:result.stepOn})
            // 加入用户操作记录
            let ustate=ulist.business.some((val)=> val.id==id)
            if(!ustate){
                ulist.business.push({id:id,flag:flag})
            }else{
                ulist.business.forEach(ele => {
                    if(ele.id==id) return ele.flag=flag
                });
            }
            let ulistUpdate=await userSer.businessSupportUpdate(decode.data,JSON.stringify(ulist.business));
            return {result,result2,ulistUpdate}
        }
        res.send({
            data:{
                support:resObj.result.support,
                stepOn:resObj.result.stepOn
            },
            meta:{
                msg: flag==true?resObj.result2==1?'赞成功':'赞失败': resObj.result2==1?'踩成功':'踩失败',
                status: resObj.result!=false?'200':'500'
            }
        })

})

// 商家信息的加入
businessRouter.post('/add',async (req,res)=>{
    let result=await businessSer.addBusiness(req.body);
    if(!result) return res.send({
        data:{},
        meta:{msg: '已有商家',status: 422}
    })
    res.send({
        data:{result},
        meta:{msg: '添加成功',status: 200}
    })
})

//商家分类的获取
businessRouter.get('/class',async (req,res)=>{
    let result=await businessSer.findClass();
    res.send({
        data:{
            result
        },
        meta:{
            msg: '商家分类获取成功',
            status: 200
        }
    })
})

//商家口碑活动的获取
businessRouter.get('/activities',async (req,res)=>{
    let {pagenum ,pagesize}=req.query;
    let result=await businessSer.findActivities(pagenum ,pagesize);
    res.send({
        data:{
            ...result
        },
        meta:{
            msg: '商家获取成功',
            status: 200
        }
    })
})

module.exports=businessRouter